import { Component, ViewChild, Injector, Output, EventEmitter, ElementRef } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { ScheduleServiceProxy, ScheduleEditDto } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';

import { CalendarModule } from 'primeng/primeng';

import * as _ from 'lodash';
import * as moment from 'moment';
import { finalize } from 'rxjs/operators';

@Component({
    selector: 'createOrEditScheduleModal',
    templateUrl: './create-or-edit-schedule-modal.component.html'
})
export class CreateOrEditScheduleModalComponent extends AppComponentBase {

    @ViewChild('createOrEditModal') modal: ModalDirective;
    @ViewChild('dateStartPicker') dateStartPicker: ElementRef;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    es: any;
    schedule: ScheduleEditDto = new ScheduleEditDto();
    workDays: boolean[] = [false, false, false, false, false, false, false];
    tr: any;
    zaoBanStartDate: Date;
    zaoBanEndDate: Date;
    zhongBanStartDate: Date;
    zhongBanEndDate: Date;
    wanBanStartDate: Date;
    wanBanEndDate: Date;

    validTimeStart: Date;
    validTimeEnd: Date;

    constructor(
        injector: Injector,
        private _scheduleService: ScheduleServiceProxy
    ) {
        super(injector);
    }
    ngOnInit() {
        this.es = {
            firstDayOfWeek: 1,
            dayNamesMin: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
            today: 'Hoy',
            clear: 'Borrar',

            dayNames: ["domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado"],
            dayNamesShort: ["dom", "lun", "mar", "mié", "jue", "vie", "sáb"],


            monthNamesShort: ["ene", "feb", "mar", "abr", "may", "jun", "jul", "ago", "sep", "oct", "nov", "dic"],

        };
    }

    show(id?: number): void {
        this.active = true;

        let days = new Array<number>();

        this._scheduleService.getEquipScheduleForEdit(id).subscribe(result => {
            if (!result.id) {
                result.workDay = '1,2,3,4,5';
                result.zaoBanStart = '08:30';
                result.zaoBanEnd = '12:30';
                result.zhongBanStart = '13:00';
                result.zhongBanEnd = '17:30';
                result.wanBanStart = '18:00';
                result.wanBanEnd = '22:00';
            }

            this.schedule = result;
            days = this.schedule.workDay ? this.schedule.workDay.split(',').map(Number) : new Array<number>();
            days.forEach((val, idx) => {
                this.workDays[val - 1] = true;
            });

            this.zaoBanStartDate = new Date('0001-01-01 ' + this.schedule.zaoBanStart);
            this.zaoBanEndDate = new Date('0001-01-01 ' + this.schedule.zaoBanEnd);
            this.zhongBanStartDate = new Date('0001-01-01 ' + this.schedule.zhongBanStart);
            this.zhongBanEndDate = new Date('0001-01-01 ' + this.schedule.zhongBanEnd);
            this.wanBanStartDate = new Date('0001-01-01 ' + this.schedule.wanBanStart);
            this.wanBanEndDate = new Date('0001-01-01 ' + this.schedule.wanBanEnd);

            this.validTimeStart = this.schedule.validTimeStart.year() === 1 ? undefined : this.schedule.validTimeStart.toDate();
            this.validTimeEnd = this.schedule.validTimeEnd.year() === 1 ? undefined : this.schedule.validTimeEnd.toDate();

            this.schedule.priority = this.schedule.priority === 0 ? 1 : this.schedule.priority;



            this.modal.show();
        });
    }

    save(): void {
        this.saving = true;

        let days: number[] = new Array<number>();
        this.workDays.forEach((val, idx) => {
            if (val) {
                days.push(idx + 1);
            }
        });

        this.schedule.workDay = days.join(',');
        this.schedule.zaoBanStart = moment(this.zaoBanStartDate).format('HH:mm');
        this.schedule.zaoBanEnd = moment(this.zaoBanEndDate).format('HH:mm');
        this.schedule.zhongBanStart = moment(this.zhongBanStartDate).format('HH:mm');
        this.schedule.zhongBanEnd = moment(this.zhongBanEndDate).format('HH:mm');
        this.schedule.wanBanStart = moment(this.wanBanStartDate).format('HH:mm');
        this.schedule.wanBanEnd = moment(this.wanBanEndDate).format('HH:mm');

        this.schedule.validTimeStart = moment(this.validTimeStart).add(8, 'hour');
        this.schedule.validTimeEnd = moment(this.validTimeEnd).add(8, 'hour');

        this._scheduleService.createOrUpdateEquipSchedule(this.schedule)
            .pipe(finalize(() => this.saving = false))
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
